package com.will.maze;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MazeActivity extends Activity implements SensorEventListener{
    /** Called when the activity is first created. */
    
	private SensorManager sManager;
	TextView xCoor,yCoor,zCoor; //Only Temporary
	RelativeLayout myRelativeLayout;
	public static float x,y,z;
	boolean isRunning = false;
	Thread backg;
	DrawView draw;
	static TextView score;
	Button next1;
	Context cxt;
	Handler h= new Handler(){
		
		@Override
		public void handleMessage(Message m){
			Random r = new Random();
			draw.isGravOn = true;
			int nextRandom = r.nextInt(4);
			if(nextRandom != draw.randomRect){
				draw.updateScore();
			}
			draw.changeWall(nextRandom);
			
		}
    };
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        cxt = this;
        setContentView(R.layout.main);
        myRelativeLayout = (RelativeLayout)findViewById(R.id.RelativeLayout1);
        draw = new DrawView(this);
        myRelativeLayout.addView(draw);
        
        sManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        
        sManager.registerListener(this,
				sManager.getDefaultSensor(Sensor.TYPE_GRAVITY),
				SensorManager.SENSOR_DELAY_NORMAL);
        
        score = (TextView)findViewById(R.id.textView1);
        score.setText(""+draw.score);
        
        next1 = (Button)findViewById(R.id.button1);
        next1.setOnClickListener(new View.OnClickListener() {
           
        	public void onClick(View view) {

        		Intent myIntente = new Intent(cxt.getApplicationContext(), TitleActivity.class);
				cxt.startActivity(myIntente);
				isRunning = false;
				onDestroy();
            }

        });
        
        
    }

	public void onAccuracyChanged(Sensor arg0, int arg1) {
		
	}

	public void onSensorChanged(SensorEvent event) {
		
		if(event.sensor.getType()==Sensor.TYPE_GRAVITY){

			//assign directions
			x=event.values[0];
			y=event.values[1];
			z=event.values[2];
			
		}
	}
	
	public static float getX(){
		return x;
	}
	
	public static float getY(){
		return y;
	}
	
	@Override
	public void onStart(){
		super.onStart();
		backg = new Thread(new Runnable(){
			public void run(){
				try{
					while(isRunning){
						Thread.sleep(3000);
						h.sendMessage(h.obtainMessage());
					}
				}catch(Throwable t){System.err.println(t.getMessage());}
			}
		});
		isRunning=true;
		backg.start();
	}
	
	@Override
	public void onDestroy(){
		super.onDestroy();
	}
}